package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.CacheKeyConstants;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WdyDeal;
import com.ruoyi.system.domain.WdyKline;
import com.ruoyi.system.mapper.WdyKlineMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.IWdyDealService;
import com.ruoyi.system.service.IWdyKlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 1分钟k线 服务层实现
 * 
 * @author ruoyi
 * @date 2020-01-10
 */
@Service
public class WdyKlineServiceImpl extends ServiceImpl<WdyKlineMapper, WdyKline> implements IWdyKlineService
{
	@Autowired
	private IWdyDealService wdyDealService;
	@Autowired
	private ISysConfigService sysConfigService;
	/**
     * 查询1分钟k线信息
     * 
     * @param id 1分钟k线ID
     * @return 1分钟k线信息
     */
    @Override
	public WdyKline selectWdyKlineById(Long id)
	{
	    return baseMapper.selectWdyKlineById(id);
	}
	
	/**
     * 查询1分钟k线列表
     * 
     * @param wdyKline 1分钟k线信息
     * @return 1分钟k线集合
     */
	@Override
	public List<WdyKline> selectWdyKlineList(WdyKline wdyKline)
	{
	    return baseMapper.selectWdyKlineList(wdyKline);
	}
	
    /**
     * 新增1分钟k线
     * 
     * @param wdyKline 1分钟k线信息
     * @return 结果
     */
	@Override
	public int insertWdyKline(WdyKline wdyKline)
	{
	    return baseMapper.insertWdyKline(wdyKline);
	}
	
	/**
     * 修改1分钟k线
     * 
     * @param wdyKline 1分钟k线信息
     * @return 结果
     */
	@Override
	public int updateWdyKline(WdyKline wdyKline)
	{
	    return baseMapper.updateWdyKline(wdyKline);
	}

	/**
     * 删除1分钟k线对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteWdyKlineByIds(String ids)
	{
		return baseMapper.deleteWdyKlineByIds(Convert.toStrArray(ids));
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public WdyKline calKline(Date calDate, int lineType) {
		String calDateStr = DateUtils.format(calDate, DateUtils.YYYY_MM_DD_HH_MM);
		String startTime = calDateStr + ":00";
		String endTime = calDateStr + ":59";
		if(lineType == WdyKline.TYPE_DAY) {
			calDateStr = DateUtils.format(calDate, DateUtils.YYYY_MM_DD);
			startTime = calDateStr + " 00:00:00";
			endTime = calDateStr + " 23:59:59";
		}
		WdyKline kline = null;
		QueryWrapper<WdyDeal> dealQuery = new QueryWrapper<>();
		dealQuery.ge("create_time", startTime);
		dealQuery.le("create_time", endTime);
		dealQuery.orderByAsc("create_time", "id");
		List<WdyDeal> dealList = wdyDealService.list(dealQuery);
		if(!CollectionUtils.isEmpty(dealList)) {
			BigDecimal open = dealList.get(0).getPrice();
			BigDecimal close = dealList.get(dealList.size() - 1).getPrice();
			BigDecimal low = open;
			BigDecimal high = open;
			BigDecimal volume = BigDecimal.ZERO;
			for(WdyDeal deal : dealList) {
				if(deal.getPrice().compareTo(low) < 0) {
					low = deal.getPrice();
				}
				if(deal.getPrice().compareTo(high) > 0) {
					high = deal.getPrice();
				}
				volume = volume.add(deal.getAmount());
			}
			kline = new WdyKline();
			kline.setOpen(open);
			kline.setClose(close);
			kline.setLow(low);
			kline.setHigh(high);
			kline.setVolumn(volume);
			kline.setStatsTime(calDate);
			kline.setLineType(lineType);
		} else {
			dealQuery = new QueryWrapper<>();
			dealQuery.lt("create_time", startTime);
			dealQuery.orderByDesc("create_time", "id");
			WdyDeal wdyDeal = wdyDealService.getOne(dealQuery);
			if(wdyDeal != null) {
				kline = new WdyKline();
				kline.setOpen(wdyDeal.getPrice());
				kline.setClose(wdyDeal.getPrice());
				kline.setLow(wdyDeal.getPrice());
				kline.setHigh(wdyDeal.getPrice());
				kline.setVolumn(BigDecimal.ZERO);
				kline.setStatsTime(calDate);
				kline.setLineType(lineType);
			}
		}
		return kline;
	}


	@Override
	public BigDecimal getYestodayPrice() {
		BigDecimal vcPrice = null;

		Date calDate = DateUtils.addDays(new Date(), -1);
		String calDateStr = DateUtils.format(calDate, DateUtils.YYYY_MM_DD);
		QueryWrapper<WdyKline> klineQuery = new QueryWrapper<>();
		klineQuery.ge("stats_time", calDateStr + " 00:00:00");
		klineQuery.le("stats_time", calDateStr + " 23:59:59");
		klineQuery.eq("line_type", WdyKline.TYPE_DAY);
		klineQuery.last("limit 1");
		WdyKline kline = this.getOne(klineQuery);
		if(kline == null) {
			kline = this.calKline(calDate, WdyKline.TYPE_DAY);
		}
		if(kline != null) {
			vcPrice = kline.getClose();
		} else {
			BigDecimal vcRatio = new BigDecimal(sysConfigService.selectConfigByKey(CacheKeyConstants.VC_RATIO));
			vcPrice = BigDecimalUtil.getP3Decimal(BigDecimalUtil.divide(1, vcRatio));
		}
		return vcPrice;
	}
}
